package Frame.battleanimation;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;

import static FCRendering.FCRMaster.fcv;

/**
 * bpbwan
 * 几乎全部的攻击动画都是在这里,普攻也是
 */
public class GunShotAni extends ABattleAni implements IBattleAni {

    //原点
    protected int[] startPoint;

    //结束点
    protected int[] endPoints;


    protected FCAnimation animation;



    protected int startFrame = 0;
    protected int flyFrame = 0;
    protected int boomFrame = 0;

    protected int[] firePaintPoints;
    protected int[] paintPoints = new int[2];
    protected int[] tmpPoint = new int[2];;
    protected int[] flyPaintStart = new int[2];

    protected int startPngIndex = -1;
    protected int flyPngIndex = -1;
    protected int flyPngIndex2 = -1;
    protected int boomPngIndex = -1;

    protected double targetDistance;
    protected String name;
    private int endFrame = 25;
    private String gunName;

    boolean hited = true;//打中的
    //这个专门是枪攻击用的动画 3段动画
    public GunShotAni(FCAnimation ani, String name, String gunName, int[] startPoint, int[] endpoint, boolean isPlayerAttackMode) {
        this.name = name;
        this.animation = ani;

        this.startPoint = startPoint;
        this.endPoints = endpoint;
        if(endpoint[0] <= 0 || endpoint[0] >= (int) (1920 * fcv.getProportion())){
            hited = false;//没打着
        }
        this.isPlayerAttackMode = isPlayerAttackMode;
        if (ani.getFirePngUrl().size() > 0) {
            startFrame = ani.fireFrameTime / 16;
            if(ani.getFirePngUrl().size() > startFrame){
                startFrame = ani.getFirePngUrl().size();
            }
        }
        if (ani.getFlyingPngUrl().size() > 0) {
            //STEP 1计算俩点之间距离，速度假设是v像素/毫秒，
            int v = (int) (700*fcv.getProportion());
            targetDistance = AnimationUtil.getDistance(startPoint, endpoint);
            flyFrame = (int) (16 * targetDistance / v);
        }
        if (ani.getBoomPngUrl().size() > 0) {
            boomFrame = ani.boomFrameTime / 16;
            if(ani.getBoomPngUrl().size() > boomFrame){
                boomFrame = ani.getBoomPngUrl().size();
            }
        }

        if(isPlayerAttackMode) {
            playerOffset[0] = (int) (20 * fcv.getProportion());
            startPoint[0] -= playerOffset[0];
        }else {
            playerOffset[0] = (int) (40 * fcv.getProportion());
            startPoint[0] += playerOffset[0];
        }
        allFrames = boomFrame + flyFrame + startFrame + endFrame;

      //  System.out.println("allFrames " + allFrames + " boomFrame " + boomFrame + " flyFrame " + flyFrame + " startFrame "
      //          + startFrame+" startPoint "+startPoint[0]+","+startPoint[1]+" endPoints "+endPoints[0]+","+endPoints[1]);
    }

    public void nextFrame(int frame) {
        //TODO 在这里必须计算好paintPoints 和startPngIndex这些
        if (frame <= startFrame && startFrame != 0) {
            int p = startFrame / animation.getFirePngUrl().size();
            int index = frame / p + 1;
            startPngIndex = Math.min(index, animation.getFirePngUrl().size() - 1);
            if (firePaintPoints == null) {
                firePaintPoints = new int[2];
                firePaintPoints[0] = startPoint[0] + (int) (animation.firePngCenterOffsetX * fcv.getProportion());
                firePaintPoints[1] = startPoint[1] + (int) (animation.firePngCenterOffsetY * fcv.getProportion());
            }
        }
        if (flyFrame != 0 && frame > startFrame - 6 && frame < (startFrame + flyFrame)) {
            int p = flyFrame / animation.getFlyingPngUrl().size();
            int index = (frame - startFrame) / p + 1;
            flyPngIndex = Math.min(index, animation.getFlyingPngUrl().size() - 1);
            flyPngIndex = Math.max(flyPngIndex, 0);

            if (frame <= startFrame && startFrame != 0) {
                //从枪口射出
                paintPoints[0] = startPoint[0] + (int) (animation.flyingPngCenterOffsetX * fcv.getProportion());
                paintPoints[1] = startPoint[1] + (int) (animation.flyingPngCenterOffsetY * fcv.getProportion());
           } else {
                startPngIndex = -1;
                AnimationUtil.getAnimatePoint(paintPoints, startPoint, endPoints, (float) (frame - startFrame) / flyFrame);
                paintPoints[0] = paintPoints[0] + (int) (animation.flyingPngCenterOffsetX * fcv.getProportion());
                paintPoints[1] = paintPoints[1] + (int) (animation.flyingPngCenterOffsetY * fcv.getProportion());
             //   System.out.println("paintPoints "+paintPoints[0]+"*"+paintPoints[1]);
            }
        } else if (boomFrame != 0 && frame >= (startFrame + flyFrame) && frame <= (allFrames-endFrame)) {
            startPngIndex = -1;
            int p = boomFrame / animation.getBoomPngUrl().size();
            int index = (frame - flyFrame - startFrame) / p + 1;
            boomPngIndex = Math.min(index, animation.getBoomPngUrl().size() - 1);
            boomPngIndex = Math.max(boomPngIndex, 0);
            flyPngIndex = -1;
            tmpPoint[0] = endPoints[0] + (int) (animation.boomPngCenterOffsetX * fcv.getProportion());
            tmpPoint[1] = endPoints[1] + (int) (animation.boomPngCenterOffsetY * fcv.getProportion());
            if(animation.getBoomPngScales() != 1) {
                double dw = (animation.getBoomPngScales() - 1)*animation.boomPngSizeW * fcv.getProportion();
                double dh = (animation.getBoomPngScales() - 1)*animation.boomPngSizeH * fcv.getProportion();
                tmpPoint[0] -= (int)(dw/2);
                tmpPoint[1] -= (int)(dh/2);
            }
            paintPoints[0] = tmpPoint[0];
            paintPoints[1] = tmpPoint[1];
         }else if(frame > (allFrames-endFrame)){
            startPngIndex = -1;
            boomPngIndex = -1;
            //爆炸了，可以将敌人闪俩下
            int cha = frame - (startFrame + flyFrame + boomFrame);
            if(hited) {//被打中才闪
                if (cha < endFrame / 4) {
                    beHitShowVisable = false;
                } else if (cha < endFrame / 2) {
                    beHitShowVisable = true;
                } else if (cha < 3 * endFrame / 4) {
                    beHitShowVisable = false;
                } else {
                    beHitShowVisable = true;
                }
            }

        }
        if (frame == 5) {
//            fcv.getMessage().add(
//                    FCRConstants.SYSTEM + FCRConstants.BATTLE + name + " " + FCRConstants.USE_QIANG);
   //         fcv.showMessage();
        }
    }

    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        if (startPngIndex != -1) {
            fire(g, fcrMaster);
        }
        if (flyPngIndex != -1) {
            flying(g, fcrMaster);
        } else if (boomPngIndex != -1) {
            boom(g, fcrMaster);
        }
    }


    @Override
    public void fire(Graphics g, FCRMaster fcrMaster) {
        g.drawImage(animation.getFirePng().get(startPngIndex), firePaintPoints[0], firePaintPoints[1],
                (int) (animation.firePngSizeW * fcv.getProportion()), (int) (animation.firePngSizeH * fcv.getProportion()), fcrMaster);
    }

    @Override
    public void flying(Graphics g, FCRMaster fcrMaster) {
        g.drawImage(animation.getFlyingPng().get(flyPngIndex), paintPoints[0], paintPoints[1],
                (int) (animation.flyingPngSizeW * fcv.getProportion()), (int) (animation.flyingPngSizeH * fcv.getProportion()), fcrMaster);
    }

    @Override
    public void boom(Graphics g, FCRMaster fcrMaster) {
        g.drawImage(animation.getBoomPng().get(boomPngIndex), paintPoints[0], paintPoints[1],
                (int) (animation.getBoomPngScales() * animation.boomPngSizeW * fcv.getProportion()), (int) (animation.getBoomPngScales() * animation.boomPngSizeH * fcv.getProportion()), fcrMaster);
    }
}
